package org.jeecg.modules.simulator.model.train.action;

import io.netty.channel.local.LocalAddress;

import java.time.LocalTime;
import java.util.*;

/**
 * @program: gp
 * @description:
 * @author: lydms
 * @create: 2024-08-08 10:11
 **/
public class TrainActions implements TrainActionRecorders {
    private boolean isUp;
    private Map<Integer, TrainActionRecorder> trainActionMap = new java.util.LinkedHashMap<>();
    private String recordersId;
    private ActionRecordersStatus status;
    private Map.Entry<Integer, TrainActionRecorder> currentEntry;
    TrainActionRecorder currentTrainActionRecorder;
    private Integer activeCount = 0;

    private String lineId;
    Iterator<Map.Entry<Integer, TrainActionRecorder>> iterator = trainActionMap.entrySet().iterator();
    String trainActionRecordersType;
    LocalTime startTime;

    public TrainActions() {

    }


    @Override
    public String getRocordersId() {
        return recordersId;
    }

    @Override
    public void setRocordersId(String recordersId) {
        this.recordersId = recordersId;

    }

    @Override
    public void setLineId(String lineId) {
        this.lineId = lineId;
    }

    @Override
    public String getLineId() {
        return this.lineId;
    }

    @Override
    public boolean isUp() {
        return isUp;
    }

    @Override
    public void setUp(boolean up) {
        this.isUp = up;
    }

    @Override
    public String getTrainActionRecordersType() {
        return trainActionRecordersType;
    }

    @Override
    public void setTrainActionRecordersType(String trainActionRecordersType) {
        this.trainActionRecordersType = trainActionRecordersType;
    }

    @Override
    public Map<Integer, TrainActionRecorder> getTrainActionRecordMap() {
        return trainActionMap;
    }

    @Override
    public void setTrainActionRecordMap(Map<Integer, TrainActionRecorder> trainActionRecordMap) {

        trainActionMap = trainActionRecordMap;
        iterator = trainActionMap.entrySet().iterator();
    }

    @Override
    public TrainActionRecorder getCurrentTrainActionRecorder() {
        if (currentEntry != null) {
            return currentEntry.getValue();
        }
        return null; // 或者抛出异常，表示当前没有记录
    }

    @Override
    public void setCurrentTrainActionRecorder(TrainActionRecorder trainActionRecorder) {
        currentTrainActionRecorder = trainActionRecorder;
    }

    @Override
    public TrainActionRecorder getFristTrainActionRecorder() {

        java.util.Optional<Integer> firstKey = trainActionMap.keySet()
                .stream().findFirst();


        TrainActionRecorder trainActionRecorder = null;
        if (firstKey.isPresent()) {
            trainActionRecorder = trainActionMap.get(firstKey.get());
            return trainActionRecorder;
        }


        return null; // 如果映射为空，返回 null

    }

    public TrainActionRecorder getSecondTrainActionRecorder() {
        List<TrainActionRecorder> records = new ArrayList<>(trainActionMap.values());
        return records.get(1);
    }

    public TrainActionRecorder getLastTrainActionRecorder() {
        List<TrainActionRecorder> records = new ArrayList<>(trainActionMap.values());
        return records.get(records.size() - 1);
    }

    @Override
    public Boolean moveNext() {
        if (iterator.hasNext()) {
            currentEntry = iterator.next();
            activeCount++;
            return true;
        }
        return false; // 没有更多的记录
    }

    @Override
    public LocalTime getEndActionTime() {
        List<TrainActionRecorder> records = new ArrayList<>(trainActionMap.values());
        TrainActionRecorder lastRecord = records.get(records.size() - 1);
        return lastRecord.getEndTime();
    }

    @Override
    public TrainActionRecorder getNextTrainActionRecorder() {
//        if (iterator.hasNext()) {
//            Map.Entry<Integer, TrainActionRecorder> entry = iterator.next();
//            return entry.getValue(); // 返回下一个 TrainActionRecorder
//        }
        return null; // 如果没有下一个记录，则返回 null
    }

    @Override
    public LocalTime getFistActionTime() {

        return startTime;
    }

    @Override
    public void setFistActionTime(LocalTime firstTime) {
        this.startTime = firstTime;

    }


    public String getLastTrainActionRecorderName() {
        Map.Entry<Integer, TrainActionRecorder> lastEntry = trainActionMap.entrySet()
                .stream()
                .reduce((first, second) -> second) // 仅保留最后一个条目
                .orElse(null);
        return lastEntry.getValue().getName();
    }


    @Override
    public void addTrainActionRecord(TrainActionRecorder trainActionRecorder) {
        trainActionMap.put(trainActionMap.size(), trainActionRecorder);
    }

    @Override
    public ActionRecordersStatus getStatus() {
        return this.status;
    }

    @Override
    public void setStatus(ActionRecordersStatus status) {
        this.status = status;
    }

    @Override
    public TrainActionRecorder getNextAction() {
        return null;
    }
}
